<?php
namespace common\component\tool;

use Yii;
use yii\web\Cookie;
/*
工具类
*/
class MyCookie
{
    private $cookies;

    public function __construct()
    {
        $this->cookies = Yii::$app->response->cookies;
    }

    /*
    设置cookie
    默认一小时后到期
    */
    public function setCookie($name, $val, $time = 3600)
    {
         $this->cookies->add(new Cookie([
            'name' => $name,
            'value' => $val,
            'expire'=> time() + $time
        ]));
    }

    /*检查cookie是否存在*/
    public function checkCookie($name)
    {
        if (isset( $this->cookies['user'])){
            return true;
        }
        return false;
    }
    

    /*取出cookie*/
    public function getCookie($name)
    {
        return  $this->cookies->get($name);//设置默认值
    }

	/*删除cookie*/
    public function delCookie($name)
    {
        $this->cookies->remove($name);
    }

}